// pages/wash/wash.js
//第三方库
import qs from "../../lib/qs/qs";
import to from "../../lib/await-to-js/await-to-js.es5";

import { tabbar_route_map, linkToTabbar } from "../../utils/route.js";
import { API_URL } from "../../api/config.js";
import api from "../../api/api.js";
import http from "../../utils/request.js";
import { formatTime } from "../../utils/util.js";
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    curTabbarKey: "wash",
    order: null,
    isAuth: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    const result = await wx.pro.getSetting();
    if (result.authSetting["scope.userInfo"]) {
      this.setData({
        isAuth: true
      });
      app.globalData.isAuth = true;
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function() {
    this.setData({
      isAuth: app.globalData.isAuth
    });

    setTimeout(() => {
      this.loadDeviceInfo();
    }, 1500);
  },

  /**
   * 与后端交互获取用户登录信息数据
   * @param {*} e
   */
  async getUserInfo() {
    wx.pro.navigateTo({
      url: "/pages/auth/auth"
    });
  },

  /**
   * 切换tabbar
   */
  handleChangeTabbar: function({ detail }) {
    let key = detail.key;
    let url = tabbar_route_map[key];
    linkToTabbar(url);
  },

  /**
   * 设备扫码
   */
  async scanToWash() {
    // console.log('scanToWash')
    const [err, data] = await to(
      wx.pro.scanCode({
        onlyFromCamera: false
      })
    );
    console.log("err", err);
    if (err && err.errMsg) {
      wx.showToast({
        title: "无法识别设备码,请重新扫码!",
        icon: "none"
      });
      return;
    }

    const { result } = data;
    let params = result.substring(result.indexOf("?") + 1);
    const query = qs.parse(params);
    console.log("query", query);
    const { did } = query;

    if (did) {
      // console.log(app.globalData)
      app.globalData.did = did;
      this.loadDeviceInfo();
      //todo 不是跳转了,而是直接请求,根据状态显示不同的列表内容
    } else {
      wx.showToast({
        title: "参数有误!"
      });
    }
  },

  async loadDeviceInfo() {
    const did = getApp().globalData.did;
    const token = wx.getStorageSync("token");
    console.log("token", token);
    // did="B110002"
    if (did) {
      const [err, result] = await to(
        http.req({
          url: api.WxWash,
          method: "POST",
          data: {
            did
          }
        })
      );
      if (err) {
        wx.showToast({
          title: "请求参数有误!",
          content: `${JSON.parse(err)}`
        });
        return;
      }
      console.log(`[请求结果${api.WxWash}]`);
      this.resetOrderInfo();
      this.computedOrderInfo(result);
    } else {
      const [err, result] = await to(
        http.req({
          url: api.WxWash,
          method: "POST",
          data: {
            did: "1"
          }
        })
      );
      if (err) {
        wx.showToast({
          title: "请求参数有误!",
          content: `${JSON.parse(err)}`
        });
        return;
      }
      // console.log("result", result);
      console.log(`请求结果${api.WxWash}`);
      console.log("result", result);
      this.resetOrderInfo();
      this.computedOrderInfo(result);
    }
  },

  /* 重置订单信息 */
  resetOrderInfo() {
    this.setData({
      order: {
        list_order: null,
        list_washmode: null,
        washer: null
      }
    });
  },

  /* 计算订单内容 */
  computedOrderInfo(washerInfo) {
    //console.log('washerInfo',washerInfo)
    const { washer, list_order } = washerInfo;
    if (washer) {
      let washertype = {
        washertype_info: "",
        kgnum: ""
      };

      let arr = washer.washertype_info.split(" ");
      washertype["washertype_info"] = arr[0] + arr[1];
      washertype["kgnum"] = arr[2];
      washer.washertype = washertype;
    }

    if (list_order && list_order.length > 0) {
      for (let i = 0; i < list_order.length; i++) {
        list_order[i].startTime = formatTime(
          new Date(list_order[i].start_time)
        );
      }
    }

    this.setData({
      order: washerInfo
    });

    console.log("[转换后结果]washerInfo", washerInfo);
  },

  /* 按钮事件 */
  reScanTap() {
    this.scanToWash();
  },
  viewOrderTap(id) {
    console.log("dingdan", id);
  },
  selectModeTap(e) {
    const _this = this;
    // console.log(e);
    const { modeId } = e.currentTarget.dataset;
    console.log(`modeId:${modeId}`);
    console.log("_this.data.order", _this.data.order);
    wx.navigateTo({
      url: `/pages/order/order?washMode=${modeId}&washerInfo=${JSON.stringify(
        _this.data.order
      )}`
    });
  },
  /* 维修电话 */
  sendAcviseTap() {
    if (this.order.washer != null) {
      //todo 报修
    } else {
      wx.showModal({
        title: "温馨提示",
        content: "请扫码选择洗衣设备！",
        showCancel: false
      });
    }
  },
  testLoadWasherInfoData() {
    //创维波轮测试
    this.setData({
      order: {
        list_order: [],
        list_washmode: [
          {
            mode_cmd: 1,
            mode_enable: 1,
            mode_hint: "加强洗",
            mode_idx: 0,
            mode_minute: 86,
            mode_money: 1,
            mode_money_yuan: 0.01,
            mode_name: "加强洗"
          },
          {
            mode_cmd: 2,
            mode_enable: 1,
            mode_hint: "标准洗",
            mode_idx: 1,
            mode_minute: 64,
            mode_money: 1,
            mode_money_yuan: 0.01,
            mode_name: "标准洗"
          },
          {
            mode_cmd: 3,
            mode_enable: 1,
            mode_hint: "快速洗",
            mode_idx: 2,
            mode_minute: 15,
            mode_money: 1,
            mode_money_yuan: 0.01,
            mode_name: "快速洗"
          },
          {
            mode_cmd: 4,
            mode_enable: 1,
            mode_hint: "单脱水",
            mode_idx: 3,
            mode_minute: 6,
            mode_money: 1,
            mode_money_yuan: 0.01,
            mode_name: "单脱水"
          }
        ],
        washer: {
          count_order: 42,
          createTime: "",
          createUserId: 0,
          createUserName: "无",
          customer_id: 13,
          customer_name: "福建因博",
          device_state: 1,
          device_state_name: "在线",
          err_msg: "",
          id: "107",
          imei: "860344047089561",
          mode_cmd: "1 2 3 4 5 6 77 8 9 10",
          mode_enable: "1 1 1 1 0 0 0 0 0 0",
          mode_hint:
            "加强洗 标准洗 快速洗 单脱水 羽绒服洗 高温杀菌 快速洗烘 大件洗烘 单烘干1 空气洗",
          mode_minute: "86 64 15 6 78 99 60 200 150 60",
          mode_money: "1 1 1 1 1 1 1 1 1 1",
          mode_name:
            "加强洗 标准洗 快速洗 单脱水 羽绒服洗 高温杀菌 快速洗烘 大件洗烘4 单烘干 空气洗",
          place_id: 13,
          place_name: "公司测试地",
          remain_min: 255,
          remark: "",
          run_state: 0,
          run_state_name: "待机中",
          state: 0,
          sum_month: "",
          sum_total: 0.51,
          updown_time: "10-20 16:23",
          washer_code: "B110001",
          washer_name: "创维波轮测试",
          washertype_id: 104,
          washertype_info: "创维 创维波轮 6.5",
          washertype: {
            kgnum: 6.5,
            washertype_info: "创维 创维波轮"
          },
          work_state: 0,
          work_state_name: "空闲"
        }
      }
    });

    // 小米机测试
    // this.setData({
    //   order: {
    //     list_order: [],
    //     list_washmode: [
    //       {
    //         mode_cmd: 1,
    //         mode_enable: 1,
    //         mode_hint: "加强洗",
    //         mode_idx: 0,
    //         mode_minute: 86,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "加强洗"
    //       },
    //       {
    //         mode_cmd: 2,
    //         mode_enable: 1,
    //         mode_hint: "标准洗",
    //         mode_idx: 1,
    //         mode_minute: 64,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "标准洗"
    //       },
    //       {
    //         mode_cmd: 3,
    //         mode_enable: 1,
    //         mode_hint: "快速洗",
    //         mode_idx: 2,
    //         mode_minute: 15,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "快速洗"
    //       },
    //       {
    //         mode_cmd: 4,
    //         mode_enable: 1,
    //         mode_hint: "单脱水",
    //         mode_idx: 3,
    //         mode_minute: 9,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "单脱水"
    //       },
    //       {
    //         mode_cmd: 7,
    //         mode_enable: 1,
    //         mode_hint: "羽绒服洗",
    //         mode_idx: 4,
    //         mode_minute: 78,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "羽绒服洗"
    //       },
    //       {
    //         mode_cmd: 8,
    //         mode_enable: 1,
    //         mode_hint: "高温杀菌",
    //         mode_idx: 5,
    //         mode_minute: 99,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "高温杀菌"
    //       },
    //       {
    //         mode_cmd: 9,
    //         mode_enable: 1,
    //         mode_hint: "快速洗烘",
    //         mode_idx: 6,
    //         mode_minute: 60,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "快速洗烘"
    //       },
    //       {
    //         mode_cmd: 10,
    //         mode_enable: 1,
    //         mode_hint: "大件洗烘",
    //         mode_idx: 7,
    //         mode_minute: 200,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "大件洗"
    //       },
    //       {
    //         mode_cmd: 11,
    //         mode_enable: 1,
    //         mode_hint: "单烘干",
    //         mode_idx: 8,
    //         mode_minute: 150,
    //         mode_money: 1,
    //         mode_money_yuan: 0.01,
    //         mode_name: "单烘干"
    //       }
    //     ],
    //     washer: {
    //       count_order: 47,
    //       createTime: "",
    //       createUserId: 0,
    //       createUserName: "无",
    //       customer_id: 13,
    //       customer_name: "福建因博",
    //       device_state: 1,
    //       device_state_name: "在线",
    //       err_msg: "",
    //       id: "108",
    //       imei: "865373045239626",
    //       mode_cmd: "1 2 3 4 7 8 9 10 11 12",
    //       mode_enable: "1 1 1 1 1 1 1 1 1 0",
    //       mode_hint:
    //         "加强洗 标准洗 快速洗 单脱水 羽绒服洗 高温杀菌 快速洗烘 大件洗烘 单烘干 空气洗",
    //       mode_minute: "86 64 15 9 78 99 60 200 150 60",
    //       mode_money: "1 1 1 1 1 1 1 1 1 1",
    //       mode_name:
    //         "加强洗 标准洗 快速洗 单脱水 羽绒服洗 高温杀菌 快速洗烘 大件洗 单烘干 空气洗",
    //       place_id: 14,
    //       place_name: "软件园公司",
    //       remain_min: 0,
    //       remark: "",
    //       run_state: 0,
    //       run_state_name: "待机中",
    //       state: 0,
    //       sum_month: "",
    //       sum_total: 0.55,
    //       updown_time: "10-14 10:34",
    //       washer_code: "B220001",
    //       washer_name: "小米机测试",
    //       washertype_id: 106,
    //       washertype_info: "小米 小米洗烘一体机 10.0",
    //       washertype: {
    //         kgnum: 10.0,
    //         washertype_info: "小米小米洗烘一体机"
    //       },
    //       work_state: 0,
    //       work_state_name: "空闲"
    //     }
    //   }
    // });
  }
});
